package com.device.controller;

import com.device.domain.dto.CabinetAddUpdateDto;
import com.device.domain.dto.CabinetDto;
import com.device.domain.vo.CabinetVo;
import com.device.service.CodenumService;
import com.device.service.ICabinetService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import com.share.common.core.context.SecurityContextHolder;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cabinet")
@Tag(name = "柜机接口管理")
public class CabinetController extends BaseController {

    @Autowired
    private ICabinetService icabinetService;

    @Autowired
    private CodenumService codenumService;

    /**
     * 柜机管理查询
     */
    @GetMapping("/list")
    @RequiresPermissions("device:cabinet:list")
    public AjaxResult list(CabinetDto cabinetDto) throws JsonProcessingException {
        PageInfo<CabinetVo> list = icabinetService.selectCabinetList(cabinetDto);
        return success(list);
    }

    /**
     * 柜机详情
     */
    @GetMapping("/{id}")
    public AjaxResult getCabinet(@PathVariable("id") Integer id){

        return success(icabinetService.getCabinet(id));
    }

    /**
     * 柜机编号查询
     */
    @GetMapping("/getCode")
    public AjaxResult getCabinetCode(){
        String cabinet = "cabinet";
        Integer integer = codenumService.getCabinetCode(cabinet);
        String format = String.format("%03d" , integer);
        String s = "gj" + format;
        return success(s);
    }

    /**
     * 搜索未使用柜机
     */
    @Operation(summary = "搜索未使用柜机")
    @GetMapping(value = "/searchNoUseList/{keyword}")
    public AjaxResult search(@PathVariable("keyword") String keyword){
        return success(icabinetService.searchNoUseList(keyword));
    }

    /**
     * 添加柜机
     */
    @PostMapping
    @RequiresPermissions("device:cabinet:add")
    public AjaxResult save(@RequestBody CabinetAddUpdateDto cabinetAddUpdateDto){
        cabinetAddUpdateDto.setCreateBy(SecurityContextHolder.getUserName());
        cabinetAddUpdateDto.setCreateTime(new Date());
        icabinetService.cabinetSave(cabinetAddUpdateDto);
        return success();
    }
    /**
     * 修改柜机
     */
    @PutMapping
    @RequiresPermissions("device:cabinet:edit")
    public AjaxResult edit(@RequestBody CabinetAddUpdateDto cabinetAddUpdateDto){
        cabinetAddUpdateDto.setUpdateBy(SecurityContextHolder.getUserName());
        cabinetAddUpdateDto.setUpdateTime(new Date());
        icabinetService.cabinetUpdate(cabinetAddUpdateDto);
        return success();
    }


    /**
     * 获取充电宝柜机全部详细信息
     */
    @Operation(summary = "获取充电宝柜机全部详细信息")
    @GetMapping(value = "/getAllInfo/{id}")
    public AjaxResult getAllInfo(@PathVariable("id") Long id) {
        return success(icabinetService.getAllInfo(id));
    }

    /**
     * 删除柜机
     */
    // 后端
    @DeleteMapping
    public AjaxResult remove(@RequestBody List<Integer> ids) {
        icabinetService.cabinetDelete(ids);
        return AjaxResult.success();
    }
    @DeleteMapping("/{id}")
    @CacheEvict(
            value= {
                    "selectCabinetList",
                    "cabinetAllInfo",
                    "cabinetDetail",
                    "noUseCabinetList"
            },
            allEntries = true
    )
    public AjaxResult add(@PathVariable("id") Integer id){
        icabinetService.removeById(id);
        return AjaxResult.success();
    }
}
